package com.xyre.client.business.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;

import com.xyre.client.R;
import com.xyre.client.framework.util.DebugLog;
import com.xyre.client.framework.util.Environment;


/**
 * Activity界面基类
 *
 * @author 792793182@qq.com 2015-06-11
 */
public abstract class BaseActivity extends FragmentActivity {

    private static final String TAG = BaseActivity.class.getSimpleName();

    private BaseView contentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = getWindow();

        //设置颜色编码格式，否则有些手机会出现颜色阶梯
        window.setFormat(PixelFormat.RGBA_8888);

        //设置背景颜色
        window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

        //去掉默认是标题，自己定义标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        contentView = new BaseView(this, true);
        contentView.setBackBtnClickListener(onClickListener);
        contentView.setRightBtnClickListener(onClickListener);
        setContentView(contentView);

        //注册网络变化的监听器
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, intentFilter);
    }

    @Override
    public final void setContentView(int layoutResID) {
        setContentView(View.inflate(this, layoutResID, null));
    }

    @Override
    public void addContentView(View view, LayoutParams params) {
        contentView.addContentView(view, params);
    }

    public void addContentView(View view) {
        contentView.addContentView(view);
    }

    public void addContentView(int layoutId) {
        contentView.addContentView(layoutId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        try {
            unregisterReceiver(receiver);
        } catch (Exception e) {
            DebugLog.e(TAG, "onDestroy()", e);
        }

        receiver = null;
        contentView = null;
    }

    @Override
    public final void setTitle(CharSequence title) {
        if (contentView != null) {
            contentView.setTitle(title);
        }
    }

    @Override
    public final void setTitle(int titleId) {
        if (contentView != null) {
            contentView.setTitle(titleId);
        }
    }

    public void onLeftBtnClick() {
        finish();
    }

    public void onRightBtnClick() {

    }

    public BaseView getContentView() {
        return contentView;
    }

    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.base_activity_head_back_btn:
                    onLeftBtnClick();
                    break;
                case R.id.base_activity_head_right_btn:
                    onRightBtnClick();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 网络变化的回调
     *
     * @param isNetworkAvailable 网络是否可用
     */
    protected void onNetworkChange(boolean isNetworkAvailable) {

    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                onNetworkChange(Environment.getInstance().isNetworkAvailable());
            }
        }
    };
}
